<template>
  <el-dialog :title="formData.accountUuid ? '修改' : '添加'" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="100px" style="padding: 0 20px"
      class="Dform">
      <el-form-item label="用户名：" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="手机号：" prop="mobileNumber">
        <el-input v-model="formData.mobileNumber" />
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item label="账号类型" prop="accountType">
        <el-select v-model="formData.accountType" @change="() => formData.accountOwner = ''" filterable clearable
          style="width: 100%" placeholder="请选择账号类型">
          <el-option :label="accountTypeMap.get(v)" :value="v"
            v-for="v in accountTypeMap.keys().filter((item) => userStore.accountType == 4 ? item == 2 : item)"
            :key="v" />
        </el-select>
      </el-form-item>
      <el-form-item label="账号所属方" prop="accountOwner" v-if="formData.accountType != 1">
        <el-select v-model="formData.accountOwner" filterable clearable style="width: 100%" placeholder="请选择账号所属方">
          <el-option :label="item.partnerName" :value="item.partnerUuid" v-for="item in partnerList"
            :key="item.partnerUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.accountUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateAccount, saveAccount, partnerQuery } from "@/api/user"
import { accountTypeMap } from '@/config/constants'
import { validateMobile } from '@/utils'
import { useUserStore } from '@/stores'

export default defineComponent({
  components: {},
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
    selectMenuList: {
      type: Array as PropType<{ title: string; id: string | number }[]>,
      default: () => [],
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const partnerList = ref<any>([])
    const userStore = useUserStore()

    watch(() => props.dialog,
      (val) => {
        Object.assign(formData, props.currentForm)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    onMounted(() => {
      partnerQuery({ pageNum: 1, pageSize: 1000 }).then((res: any) => {
        partnerList.value = res.data.list || []
      })
    })

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          if (formData.accountUuid) {
            updateAccount(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('修改成功')
                close()
              }

            })
          } else {
            saveAccount(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('添加成功')
                close()
              }
            })
          }
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }
    const rules = reactive({
      accountOwner: [
        { required: true, message: '请选择账号所属方', trigger: 'blur' }
      ],
      accountType: [
        { required: true, message: '请选择账号类型', trigger: 'blur' }
      ],
      name: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      mobileNumber: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { validator: validateMobile, }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ],
    })

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      accountTypeMap,
      partnerList,
      userStore,
      close,
      resetForm,
      onSubmit,
      rules,
    }
  },
})
</script>

<style></style>
